package com.gouda.web.controller;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gouda.web.constant.Constant;
import com.gouda.web.exception.ErrorCode;
import com.gouda.web.exception.RequestException;
import org.apache.log4j.Logger;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: souu
 * Date: 15-3-30
 * Time: 上午11:02
 * To change this template use File | Settings | File Templates.
 */
public class AbstractController {
    public static final Logger LOG = Logger.getLogger(AbstractController.class);

    protected static final ObjectMapper jsonMapper = new ObjectMapper();
    private static int sessionExpire;

    @ModelAttribute
    public void init(ModelMap model, HttpServletRequest request) {
        model.put("ctx", request.getContextPath());
    }

    /**
     * set return data
     */
    protected void returnData(HttpServletRequest req, Object data) {
        req.setAttribute(Constant.WEBKEY_OUTPUTDATA, data);
    }

    /**
     * 获取消息头.
     *
     * @param req
     * @return
     */
    protected Map<String, String> getHeader(HttpServletRequest req) {
        return (Map<String, String>) req.getAttribute(Constant.WEBKEY_HEADER);
    }

//    protected CommonHeader getCommonHeader(HttpServletRequest req) {
//        Map<String, String> header = getHeader(req);
//        String uid = header.get("uid");
//        String cityid = header.get("cityid");
//        CommonHeader commonHeader = CommonHeader.valueOf(uid, cityid);
//        return commonHeader;
//    }

    /**
     * get request body as string.
     */
    protected String getBodyAsString(HttpServletRequest req) {
        return (String) req.getAttribute(Constant.WEBKEY_BODY);
    }

    /**
     * get request body as map.
     */
    protected Map<String, String> getBodyAsMap(HttpServletRequest req) {
        String content = (String) req.getAttribute(Constant.WEBKEY_BODY);
        try {
            return (Map<String, String>) jsonMapper.readValue(content, Map.class);
        } catch (Exception e) {
            throw new RequestException(ErrorCode.EC_400000);
        }
    }

    protected Map<String, Object> getBodyAsMap2(HttpServletRequest req) {
        String content = (String) req.getAttribute(Constant.WEBKEY_BODY);
        try {
            return (Map<String, Object>) jsonMapper.readValue(content, Map.class);
        } catch (Exception e) {
            throw new RequestException(ErrorCode.EC_400000);
        }
    }

    protected Map getBodyAsMap2(HttpServletRequest req, TypeReference tr) {
        String content = (String) req.getAttribute(Constant.WEBKEY_BODY);
        try {
            return jsonMapper.readValue(content, tr);
        } catch (Exception e) {
            throw new RequestException(ErrorCode.EC_400000);
        }
    }

    protected <T> T getBodyAsObject(HttpServletRequest req, Class<T> clazz) {
        String content = (String) req.getAttribute(Constant.WEBKEY_BODY);
        try {
            return (T) jsonMapper.readValue(content, clazz);
        } catch (Exception e) {
            RequestException re = new RequestException(ErrorCode.EC_400000);
            re.setMessage(content);
            throw re;
        }
    }
}
